app.views.ItemEditView = Ext.extend(Ext.form.FormPanel, {
	dockedItems: [{
		title: 'Check Lists',
		xtype: 'toolbar',
		items: [
			
			{
				id: 'cancelUpdate',
				text : 'back',
				ui: 'back',
				listeners: {
					'tap': function() {
						Ext.dispatch({
							controller: app.controllers.ListController,
							id: app.views.ItemEditView.record.getId(),
							action: 'displayDetail',
							animation: {type: 'slide', direction: 'right'}
						});
					}
				}
			},
			
			{xtype: 'spacer'},
			
			{
				text : 'save',
				iconMask: true,
				listeners: {
					'tap': function() {
					
						var itemVal = app.views.ItemEditView.getValues().item;
						
						console.log('new item value: ' + itemVal);
						
						var rcd = app.views.ItemEditView.record;
						var itemId = app.views.ItemEditView.itemId;
						
						var oldVal = '';
						
						var items = rcd.get('items');
						
						for (var k = 0; k < items.length; k++) {
							var item = items[k];
			
							if (item.get('itemOrder') == itemId) {
								oldVal = item.get('item');
							}
						}

						if (itemVal && itemVal != oldVal) {
							var rcds = [];
							
							var params = {
								liId: rcd.get('id'),
								itemId: itemId,
								item: itemVal
							};
							
							rcds.push(params);
							
							var operation = new Ext.data.Operation({
								action: 'updateItemValue',
								records: rcds
							});
								
							var theProxy = app.stores.checkLists.getProxy();
							
							theProxy.update(operation, app.views.ItemEditView.updateWithRecord, app.views.ItemEditView);

						}
						
					}
				}
			}
			
		]
		
	},
	
	{
		dock : 'bottom',
		xtype: 'toolbar',
		ui   : 'light',
		items: [
			
			{
				iconCls: 'trash',
				iconMask: true,
				ui: 'plain',
				text: 'delete',
				
				listeners: {
					'tap': function() {
						var rcd = app.views.ItemEditView.record;
						var itemId = app.views.ItemEditView.itemId;
						var liId = rcd.get('id');
						
						var params = {
							liId: liId,
							itemId: itemId
						};
						
						var rcds = [];
						
						rcds.push(params);
							
						var operation = new Ext.data.Operation({
							action: 'removeSingleItem',
							records: rcds
						});
						
						var theProxy = app.stores.checkLists.getProxy();
							
						theProxy.update(operation, null, null);
						
						Ext.dispatch({
							controller: app.controllers.ListController,
							id: app.views.ItemEditView.record.getId(),
							action: 'displayDetail',
							animation: {type: 'slide', direction: 'right'}
						});
						
					}
				}
				
			}
		]
	}],
	
	submitOnAction: false,
	
	items: [
		{
			name: 'item',
			label: 'Item',
			xtype: 'textareafield',
			maxRows: 5,
			maxLength: 128
		}
			
	],
				
	updateWithRecord: function(record, itemId) {
				
		var toolbar = this.getDockedItems()[0];
		toolbar.setTitle(record.get('listName'));
		
		var items = record.get('items');
						
		for (var k = 0; k < items.length; k++) {
			var item = items[k];
			
			if (item.get('itemOrder') == itemId) {
				this.setValues({item: item.get('item')});
			}
		}
		
		
		
		this.record = record;
		this.itemId = itemId;
	}
	
});


